<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>
<body>
    <!-- 
        常用的生命周期钩子:
            1.mounted:发送ajax请求,启动定时器,绑定自定义事件,订阅消息等[初始化操作]
            2.beforeDestory:清除定时器,解绑自定义事件，取消订阅消息等[收尾工作]

        关于销毁vue实例
            1.销毁后借助Vue开发者工具不会看到任何信息
            2.销毁后自定义事件会失效，但原生dom事件依然有效
            3.一般不会再beforeDestory操作数据,因为即使操作数据，也不会触发更新流程了
     -->

    <div id="root">
        <h2 :style="{opacity}">欢迎学习vue</h2>
        <button @click="opacity = 1">透明度设置为1</button>
        <button @click="stop">点我停止变化</button>
    </div>
    <script>
        Vue.config.productionTip = false
        const vm =new Vue({
            el:'#root',
            data: {
                opacity: 1,
                timer: 0
            },
            methods: {
                stop () {
                    this.$destory()
                }
            },
            //Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted
            mounted () {
                this.timer = setInterval(() => {
                        console.log('setInterval')
                        this.opacity -= 0.01
                        if(this.opacity <= 0) {
                            this.opacity = 1
                        }
                    }, 16);
            },
            beforeDestroy() {
                console.log('vm即将驾鹤西游')
                clearInterval(this.timer)
            },
        })
    </script>
</body>
</html>